package com.alei.redis.redisdemo.service.impl;

import com.alei.redis.redisdemo.bean.Commodity;
import com.alei.redis.redisdemo.bean.User;
import com.alei.redis.redisdemo.mapper.UserMapper;
import com.alei.redis.redisdemo.service.UserService;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-06-16 18:35
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private JedisPool jedisPool;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public Commodity getCommById(Integer id) {
        return userMapper.getCommById(id);
    }

    @Override
    public String jedisDemo() {
        Jedis jedis = null;
        try {
            //1.从连接池获取jedis对象
            jedis = jedisPool.getResource();
            //2.执行操作
            jedis.set("alei", "ayu");
            return jedis.get("alei");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != jedis) {
                //使用  jedisPool.close() 操作不是关闭连接，代表归还连接池
                jedisPool.close();
            }
        }
        return null;
    }
}
